package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import uteis.Constantes;
import model.TpServico;

public class TpServicoJDBCCadastro implements TpServicoCadastroDAO {

	Connection conexao;

	public TpServicoJDBCCadastro(Connection conexao) {
		this.conexao = conexao;
	}

	public String salvar(TpServico tpservico) {
		String sqlquery = "insert into TPSERVICO (NMTPSERVICO, USUINCLUSAO, COMPINCLUSAO, USUALTERACAO, COMPALTERACAO, DTINCLUSAO, "
				+ "VLBASE, CDEMPRESA) values(?,?,?,?,?,?,?,?);";

		Date datenow = null;
		try {
			String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			datenow = dateFormat.parse(date);
			// beneficiarioLista.get(tamanho).getDtinclusao(datenow);
		} catch (Exception e) {
			e.printStackTrace();
		}

		PreparedStatement pstmt;
		try {

			pstmt = this.conexao.prepareStatement(sqlquery);
			pstmt.setString(1, tpservico.getNmtpservico());
			pstmt.setString(2, tpservico.getUsuinclusao());
			pstmt.setString(3, tpservico.getCompinclusao());
			pstmt.setString(4, tpservico.getUsualteracao());
			pstmt.setString(5, tpservico.getCompalteracao());
			pstmt.setDate(6, new java.sql.Date(datenow.getTime()));
			// pstmt.setTime(8, new
			// java.sql.Time(formapagto.getHrinclusao().getTime()));
			// pstmt.setTime(10, new
			// java.sql.Time(formapagto.getHralteracao().getTime()));
			pstmt.setDouble(7, tpservico.getVlbase());
			pstmt.setString(8, Constantes.cdempresas);
			pstmt.execute();

			return ("success");

		} catch (Exception e) {
			e.printStackTrace();
			return ("error");
		}
	}

	public ResultSet ResultTpSevico(String cdinstcobra) {

		// List<String> result = new ArrayList<String>();
		ResultSet valueinstcobra = null;
		// if (!nmformapagto.trim().equals("")) {
		String sql = "select CDTPSERVICO, NMTPSERVICO from TPSERVICO where CDTPSERVICO in("
				+ "select CDTPSERVICO from SERVICO where CDINSTCOBRANCA = "
				+ cdinstcobra
				+ " AND CDEMPRESA = "
				+ Constantes.cdempresas
				+ ");";
		try {
			java.sql.Statement stmt = conexao.createStatement();
			valueinstcobra = stmt.executeQuery(sql);

		} catch (Exception e) {
			e.printStackTrace();
		}
		/**
		 * }else { System.out.println("VALOR EM BRANCO "+nmformapagto+
		 * " ------------- "+result ); }
		 **/
		return valueinstcobra;
	}

	public List<String> ListarTpServico(String nmtpservico) {

		List<String> result = new ArrayList<String>();
		if (!nmtpservico.trim().equals("")) {
			String sql = "SELECT CDTPSERVICO, NMTPSERVICO FROM TPSERVICO WHERE NMTPSERVICO like '%"
					+ nmtpservico
					+ "%' AND CDEMPRESA = "
					+ Constantes.cdempresas + " ORDER BY NMTPSERVICO ASC;";
			String snmtpservico = "";
			String scdtpservico = "";
			try {
				java.sql.Statement stmt = conexao.createStatement();
				ResultSet valuetpservico = stmt.executeQuery(sql);

				valuetpservico.first();
				while (!valuetpservico.isAfterLast()) {
					snmtpservico = valuetpservico.getString("NMTPSERVICO");
					scdtpservico = valuetpservico.getString("CDTPSERVICO");
					result.add(snmtpservico);
					// result.add(scdtpservico+" | "+snmtpservico);
					valuetpservico.next();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("VALOR EM BRANCO " + nmtpservico
					+ " ------------- " + result);
		}
		return result;
	}

	public boolean localizarTpServico(String tpservico) {
		String sqlquery = "SELECT NMTPSERVICO FROM TPSERVICO WHERE (NMTPSERVICO like '"
				+ tpservico
				+ "' and CDEMPRESA = "
				+ Constantes.cdempresas
				+ ") ORDER BY NMTPSERVICO ASC;";
		try {
			String valor = "";
			java.sql.Statement stmt = conexao.createStatement();
			ResultSet rs = null;
			rs = stmt.executeQuery(sqlquery);
			while (rs.next()) {
				valor = rs.getString("NMTPSERVICO");
			}
			if (valor.equals("")) {
				return (false);
			} else {
				return (true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return (false);
		}
	}

	public String LocalizaTpServico(String nmtpservico) {

		String result = "";
		if (!nmtpservico.trim().equals("")) {

			String sql = "SELECT CDTPSERVICO FROM TPSERVICO WHERE (NMTPSERVICO like '"
					+ nmtpservico
					+ "%' AND CDEMPRESA = "
					+ Constantes.cdempresas + ") ORDER BY CDTPSERVICO ASC;";
			String scdtpservico = "";
			try {
				java.sql.Statement stmt = conexao.createStatement();
				ResultSet valuetpservico = stmt.executeQuery(sql);

				valuetpservico.first();
				scdtpservico = valuetpservico.getString("CDTPSERVICO");
				// result.add(scdregistro);
				result = (scdtpservico);
				System.out.println("TP SERVICO " + scdtpservico
						+ " ------------- " + result);

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("VALOR EM BRANCO " + nmtpservico
					+ " ------------- " + result);
		}
		return result;
	}

	public void editar(TpServico tpservico) {
	}

	public void excluir(TpServico tpservico) {
	}

	public List<TpServico> ListarTpServicoT() {
		return null;
	}

}
